#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage:"
    echo "  case.sh add <caseNO>    - Add case demo to build"
    echo "  case.sh clear           - Clear all case configurations"
    echo "  case.sh new <typeName-caseNO-desc>    - Create new case demo"
    echo "  case.sh buildall        - Build demos hap with all cases"
    echo "  case.sh build <caseNO>  - Build demos hap with specified cases"
    exit 1
fi

if [ "$1" == "add" ]; then
    if [ -z "$2" ]; then
        echo "Error: caseNO is required"
        echo "Example: case.sh add case001"
        exit 1
    fi
    echo "Adding case demo $2..."
    hvigorw caseAdd -p caseNO=$2
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "clear" ]; then
    echo "Clearing case configurations..."
    hvigorw caseClear
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "new" ]; then
    if [ -z "$2" ]; then
        echo "Error: demoname is required"
        echo "Example: case.sh new ArkUI-case001-test"
        exit 1
    fi
    echo "New case demo $2..."
    hvigorw generateModule -p demoname=$2
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    exit 0
fi

if [ "$1" == "buildall" ]; then
    echo "Execute clear case demos...."
    hvigorw caseClear
    echo "Execute add all case demos...."
    hvigorw caseAddAll
    echo "Execute install hars...."
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    echo "Execute build hap...."
    hvigorw --mode module -p module=default@default -p product=default assembleHap
    exit 0
fi

if [ "$1" == "build" ]; then
    if [ -z "$2" ]; then
        echo "Error: caseNO is required"
        echo "Example: case.sh build case001"
        exit 1
    fi
    echo "Execute clear case demos...."
    hvigorw caseClear
    echo "Execute add case demo $2..."
    hvigorw caseAdd -p caseNO=$2
    echo "Execute install hars...."
    ohpm install --all --registry https://ohpm.openharmony.cn/ohpm/ --strict_ssl false
    hvigorw --sync
    echo "Execute build hap...."
    hvigorw --mode module -p module=default@default -p product=default assembleHap
    exit 0
fi

echo "Error: Unknown command '$1'"
echo "Valid commands are: add, clear, new, buildall, build"
exit 1